package com.zuorenke.exception;

import com.zuorenke.help.MyCollectionUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @author Z.R.K
 * @description
 * @create 2019-05-20 14:06:53
 **/
public abstract class Validation {
	public static void isForbidden(boolean expression) {
		if (expression) {
			throw new ForbiddenException();
		}
	}
	
	public static void isPageNotFound(boolean expression) {
		if (expression) {
			throw new PageNotFoundException();
		}
	}
	
	public static void isTrue(boolean expression, String message, Object... args) {
		if (!expression) {
			throwValidException(message, args);
		}
		
	}
	
	public static void isFalse(boolean expression, String message, Object... args) {
		if (expression) {
			throwValidException(message, args);
		}
		
	}
	
	public static void isNull(Object object, String message, Object... args) {
		if (object != null) {
			throwValidException(message, args);
		}
		
	}
	
	public static void notNull(Object object, String message, Object... args) {
		if (object == null) {
			throwValidException(message, args);
		}
		
	}
	
	public static void hasLength(String text, String message, Object... args) {
		if (!StringUtils.hasLength(text)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void hasText(String text, String message, Object... args) {
		if (!StringUtils.hasText(text)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void doesNotContain(String textToSearch, String substring, String message, Object... args) {
		if (StringUtils.hasText(textToSearch) && StringUtils.hasText(substring) && textToSearch.contains(substring)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void notEmpty(Object[] array, String message, Object... args) {
		if (MyCollectionUtils.isEmpty(array)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void noNullElements(Object[] array, String message, Object... args) {
		if (array != null) {
			Object[] var3 = array;
			int var4 = array.length;
			
			for(int var5 = 0; var5 < var4; ++var5) {
				Object element = var3[var5];
				if (element == null) {
					throwValidException(message, args);
				}
			}
		}
		
	}
	
	public static void notEmpty(Collection<?> collection, String message, Object... args) {
		if (CollectionUtils.isEmpty(collection)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void notEmpty(Map<?, ?> map, String message, Object... args) {
		if (CollectionUtils.isEmpty(map)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void isInstanceOf(Class<?> type, Object obj, String message, Object... args) {
		if (!type.isInstance(obj)) {
			throwValidException(message, args);
		}
		
	}
	
	public static void throwValidException(String message, Object... args) {
		throw new ValidateException(message, args);
	}
}